﻿using System;
using System.Reflection;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// json值转换器基类
    /// </summary>
    public abstract class JsonValueConverterAbs : IJsonValueConverter
    {
        private readonly bool _hasAddDoubleQuotes;
        /// <summary>
        /// 值是否需要添加双引号[true:添加双引号;false:不添加双引号]
        /// </summary>
        public bool HasAddDoubleQuotes
        {
            get { return this._hasAddDoubleQuotes; }
        }








        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="hasAddDoubleQuotes">值是否需要添加双引号[true:添加双引号;false:不添加双引号]</param>
        public JsonValueConverterAbs(bool hasAddDoubleQuotes)
        {
            this._hasAddDoubleQuotes = hasAddDoubleQuotes;
        }









        /// <summary>
        /// 尝试将对象值转换为json值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="value">属性或字段值</param>
        /// <param name="jsonValue">成员值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        public bool TryConvertTo(MemberInfo memberInfo, Type valueType, object owner, object value, out string jsonValue)
        {
            try
            {
                return this.PrimitiveTryConvertTo(memberInfo, valueType, owner, value, out jsonValue);
            }
            catch (Exception ex)
            {
                jsonValue = ex.ToString();
                return false;
            }
        }

        /// <summary>
        /// 尝试将对象值转换为json值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="value">属性或字段值</param>
        /// <param name="jsonValue">成员值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected virtual bool PrimitiveTryConvertTo(MemberInfo memberInfo, Type valueType, object owner, object value, out string jsonValue)
        {
            throw new NotImplementedException();
        }







        /// <summary>
        /// 尝试将json值转换为对象值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="jsonValue">json值</param>
        /// <param name="value">对象值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        public bool TryConvertFrom(MemberInfo memberInfo, Type valueType, object owner, string jsonValue, out object value)
        {
            try
            {
                return this.PrimitiveTryConvertFrom(memberInfo, valueType, owner, jsonValue, out value);
            }
            catch (Exception ex)
            {
                value = ex;
                return false;
            }
        }

        /// <summary>
        /// 尝试将json值转换为对象值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="jsonValue">json值</param>
        /// <param name="value">对象值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected virtual bool PrimitiveTryConvertFrom(MemberInfo memberInfo, Type valueType, object owner, string jsonValue, out object value)
        {
            throw new NotImplementedException();
        }
    }
}
